/**
 * 数据库
 */

import { DB_CONFIG, OPEN_LOG } from '../server.config.js';
import Mysql from 'node-mysql-promise';
import _ from 'underscore';
import { dateFormat } from './functions';

const log = function () {
	console.log(dateFormat('YYYY-MM-DD hh:mm:ss', new Date()), ...arguments);
};
const mysqlPromise = Mysql.createConnection(DB_CONFIG);
export default {
	table(tableName) {
		if (OPEN_LOG) log();
		return mysqlPromise.table(tableName);
	},
	query(sql, parse) {
		return mysqlPromise.query(sql, parse);
	},
	execute(sql, parse) {
		return mysqlPromise.execute(sql, parse);
	},
	dataFormat(data) {
		let params = _.clone(data);
		// params.updated = formatter.datetime(new Date())
		delete params.create_time;
		delete params.id;
		return params;
	},
};
